/**
 * Copyright 2009 the original author or authors.
 *
 * Licensed under the GPL License, Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.gnu.org/licenses/lgpl-3.0.txt
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.guardianapis.api.util;

import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class StreamLogger {

	/*******************************************************************************
	 * Constants
	 *******************************************************************************/

	private static Log log = LogFactory.getLog(StreamLogger.class);
	private static final String EMPTY_RESPONSE = "";

	/*******************************************************************************
	 * Public Methods
	 *******************************************************************************/

	public static String getStreamCharacters(InputStream pStream, int pNumberOfCharactersToRead) {
		String streamCharacters = null;
			
		try {
			// We don't want to interfere with the stream before it is read by the serialier, so we check that we can
			// return it to it's initial state before we attempt to extract a logging string from it
			
			if (pStream != null) {
				if (pStream.markSupported()) {
					
					// First we mark the current position in the stream so that we can return to it, setting a mark limit
					// beyond the number of characters that we will be reading
					
					pStream.mark(pNumberOfCharactersToRead + 1);
					
					// Then we read the specified numer of characters from the stream and return back to the marked position
					
					byte[] charBuffer = new byte[pNumberOfCharactersToRead];
					pStream.read(charBuffer);
					streamCharacters = new String(charBuffer);
					pStream.reset();
				} else {
					log.info("Can't log stream characters - stream of type: " + pStream.getClass() + " does not support reset");
					streamCharacters = EMPTY_RESPONSE;
				}
			} else {
				streamCharacters = EMPTY_RESPONSE;
			}
		} catch (IOException e) {
			log.error("Error while attempting to get: " + pNumberOfCharactersToRead + " characters from stream: " + pStream);
		}
		return stripNewLines(streamCharacters);
	}
	
	/*******************************************************************************
	 * Private Methods
	 *******************************************************************************/
	
	public static String stripNewLines(String pSourcePath) {
		String pass1 = pSourcePath.replaceAll("[\\n]+","");
		String pass2 = pass1.replaceAll("[\\r]+","");
	    return pass2; 
	}
	
	
}
